package com.ubst.smartad.impl;

import java.sql.*;
import java.util.*;
import java.io.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import com.ubst.smartad.dao.TSAUserGroupDO;
import com.ubst.smartad.common.TimeUtil;
import com.ubst.smartad.logger.Debug;

public class TSAUserGroupMngImpl {
	
	Connection con = null;
	private String serverName;
	private Debug debug;

	/** 
     * 생성자
     *   
     *   
    */ 
	public TSAUserGroupMngImpl(Connection con, String serverName, String log_level) {
		this.con = con;
		this.serverName = serverName;
		this.debug = new Debug("TSAUserGroupMngImpl", log_level);

	}
	
    /** 
	 * @author KJH
     * 계정관리 사용자 그룹등록
     *   
     */  
    public void insertUserGroup(TSAUserGroupDO userGroupDO) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {

			long l_cur_date = TimeUtil.getCurrentTime();

            queryBuf = new StringBuffer();
			queryBuf.append("INSERT INTO TSA_USER_GROUP " )
				.append(" ( ")
				.append("  UGROUPID ,UPGROUPID ,UGROUPNAME ,SCOMMENT ,CREATEDATE ")
				.append(" ) ")
				.append(" VALUES")
				.append(" ( ") 
				.append("  ? ,? ,? ,? ,? ")
				.append(" ) ");

			debug.debug("insertUserGroup ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, userGroupDO.ugroupid);
			pstmt.setString(idx++, userGroupDO.upgroupid);
			pstmt.setString(idx++, userGroupDO.ugroupname);
			pstmt.setString(idx++, userGroupDO.scomment);
			pstmt.setString(idx++, TimeUtil.getDateString(l_cur_date, "yyyyMMddHHmmss"));

            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }

    /** 
	 * @author KJH
     * 사용자 그룹정보 변경
     *   
     */  
    public void updateUserGroup(TSAUserGroupDO userGroupDO) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {

			long l_cur_date = TimeUtil.getCurrentTime();

            queryBuf = new StringBuffer();
			queryBuf.append("UPDATE TSA_USER_GROUP " )
				.append(" SET ")
				.append("  UPGROUPID=? ,UGROUPNAME=? ,SCOMMENT=? ")
				.append(" WHERE UGROUPID=? ");

			debug.debug("updateUserGroup ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, userGroupDO.upgroupid);
			pstmt.setString(idx++, userGroupDO.ugroupname);
			pstmt.setString(idx++, userGroupDO.scomment);
			pstmt.setString(idx++, userGroupDO.ugroupid);

            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }

	/**
	 * @author : KJH 
	 * 사용자 그룹정보 출력
	 *
	 */
	public TSAUserGroupDO selectUserGroup(String ugroupid) throws SQLException {

		PreparedStatement pstmt=null;
		ResultSet rs = null;

		TSAUserGroupDO userGroupDO = null;

		try {
			
			StringBuffer queryBuf = new StringBuffer();
			queryBuf.append("SELECT ")
				.append("  UPGROUPID ,UGROUPNAME ,SCOMMENT ,CREATEDATE ")
				.append(" FROM TSA_USER_GROUP ")
				.append(" WHERE UGROUPID=? ");

			debug.debug("selectUserGroup ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, ugroupid);

			rs = pstmt.executeQuery();

			if ( rs.next() ){
				userGroupDO = new TSAUserGroupDO();
				userGroupDO.ugroupid = ugroupid;
				userGroupDO.upgroupid = rs.getString("UPGROUPID");
				userGroupDO.ugroupname = rs.getString("UGROUPNAME");
				userGroupDO.scomment = rs.getString("SCOMMENT");
				userGroupDO.createdate = rs.getString("CREATEDATE");
			}

			rs.close();
			pstmt.close();

		}catch (SQLException e){
			throw e;
		}finally{
			if ( rs != null) try{rs.close();}catch(SQLException ee){}
			if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
		}

		return userGroupDO;
	}

	/**
	 * @author : KJH 
	 * 사용자 그룹정보 리스트 출력
	 *
	 */
	public Vector selectUserGroupList() throws SQLException {

		PreparedStatement pstmt=null;
		ResultSet rs = null;

		Vector userGroupDOList = new Vector();
		TSAUserGroupDO userGroupDO = null;

		try {
			
			StringBuffer queryBuf = new StringBuffer();
			queryBuf.append("SELECT ")
				.append("  UGROUPID ,UPGROUPID ,UGROUPNAME ,SCOMMENT ,CREATEDATE ")
				.append(" FROM TSA_USER_GROUP ");

			debug.debug("selectUserGroupList ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );

			rs = pstmt.executeQuery();

			while ( rs.next() ){
				userGroupDO = new TSAUserGroupDO();
				userGroupDO.ugroupid = rs.getString("UGROUPID");
				userGroupDO.upgroupid = rs.getString("UPGROUPID");
				userGroupDO.ugroupname = rs.getString("UGROUPNAME");
				userGroupDO.scomment = rs.getString("SCOMMENT");
				userGroupDO.createdate = rs.getString("CREATEDATE");

				userGroupDOList.add(userGroupDO);
				userGroupDO = null;
			}

			rs.close();
			pstmt.close();

		}catch (SQLException e){
			throw e;
		}finally{
			if ( rs != null) try{rs.close();}catch(SQLException ee){}
			if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
		}

		return userGroupDOList;
	}

	/**
	 * @author : KJH 
	 * 그룹명 중복검사.
	 *
	 */
	public boolean checkDupGroupName(String ugroupname) throws SQLException {

		PreparedStatement pstmt=null;
		ResultSet rs = null;

		boolean ret_bool = false;

		try {

			StringBuffer qrybuf = new StringBuffer();
			qrybuf.append("SELECT ")
				.append("  UGROUPID ")
				.append(" FROM TSA_USER_GROUP ")
				.append(" WHERE UGROUPNAME=? ");

			pstmt = con.prepareStatement( qrybuf.toString() );
			pstmt.setString(1, ugroupname);

			rs = pstmt.executeQuery();

			if ( rs.next() ){
				ret_bool = true;
			}

			rs.close();
			pstmt.close();

		}catch (SQLException e){
			throw e;
		}finally{
			if ( rs != null) try{rs.close();}catch(SQLException ee){}
			if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
		}

		return ret_bool;
	}

    /** 
	 * @author KJH
     * 시스템 그룹삭제
     *   
     */  
    public void deleteUserGroup(String ugroupid) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {

            queryBuf = new StringBuffer();
			queryBuf.append("DELETE FROM TSA_USER_GROUP " )
				.append(" WHERE UGROUPID=? ");

			debug.debug("deleteUserGroup ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, ugroupid);

            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }

}
